<template>
  <el-row type="flex" class="page-container">
    <el-col :span="8" class="aside">
      <p>家医管理系统</p>
      <p class="small">Family medicine management system</p>
      <img src="@/assets/index_images/login-aside.jpg" alt="" />
      <!-- <div><span>版本号：1.1.1.210430_beta</span></div> -->
    </el-col>
    <el-col :span="16">
      <div class="login-container">
        <el-card class="login-card">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            autocomplete="on"
            label-position="left"
          >
            <p>Sing in</p>
            <el-form-item prop="account">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                ref="account"
                v-model="loginForm.account"
                placeholder="account"
                name="account"
                type="text"
                tabindex="1"
                autocomplete="on"
              />
            </el-form-item>

            <el-tooltip
              v-model="capsTooltip"
              content="Caps lock is On"
              placement="right"
              manual
            >
              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="Password"
                  name="password"
                  tabindex="2"
                  autocomplete="on"
                  @keyup.native="checkCapslock"
                  @blur="capsTooltip = false"
                  @keyup.enter.native="handleLogin"
                />
                <span class="show-pwd" @click="showPwd">
                  <svg-icon
                    :icon-class="
                      passwordType === 'password' ? 'eye' : 'eye-open'
                    "
                  />
                </span>
              </el-form-item>
            </el-tooltip>

            <el-button
              :loading="loading"
              type="primary"
              style="width: 100%; margin-bottom: 30px"
              @click="handleLogin"
              >登录</el-button
            >
       <!--     <el-button type="text" class="reg" @click="register"
              >用户注册</el-button
            > -->
          </el-form>
        </el-card>
      </div>
    </el-col>
    <regDia ref="regDia" @transData="transData"></regDia>
  </el-row>
</template>

<script>
import regDia from "./components/register";
import { login } from "@/api/family_doctor/family_doctor";
export default {
  name: "Login",
  components: { regDia },
  data() {
    return {
      loginForm: {
        account: undefined,
        password: undefined,
        dataSource: 1,
      },
      loginRules: {
        account: [{ required: true, trigger: "blur" }],
        password: [{ required: true, trigger: "blur" }],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {
    if (this.loginForm.account === "") {
      this.$refs.account.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          login(this.loginForm).then((res) => {
            if (res.code == "AA000000") {
              this.$store.commit('saveToken',res.data.token)
              this.$store.commit('saveMenus',res.data.menus)
              this.$store.commit('savePermission',res.data.permission)
              this.$store.commit('saveName',res.data.name)
              this.$store.commit('saveRoleTypes',res.data.roleTypes)
              this.$router.push('/go');
              location.reload();
            }
            this.loading = false;
          });
        } else return false;
      });
	  this.loading = false;
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
    register() {
      this.$refs.regDia.openDialog();
    },
    transData() {},
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #fff;
$light_gray: #fff;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #333;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.9);
    border: 1px solid rgb(173, 172, 172);
    // background: rgba(43, 28, 28, 0.1);
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #fff;
$dark_gray: #889aa4;
$light_gray: #eee;

.page-container {
  height: 100%;

  .aside {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #1a305f;
    text-align: center;
    padding: 100px 0;

    p {
      color: #fff;
      font-size: 30px;
      margin-bottom: 0;
    }
    span {
      color: #fff;
      font-size: 10px;
      position: absolute;
      left: 16%;
      top: 90%;
      transform: translate(-50%, 0);
    }
    .small {
      font-size: 16px;
      color: rgb(146, 140, 140);
    }

    img {
      width: 60%;
      margin-top: 100px;
    }
  }
}

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    // padding: 20px 20px 20px;
    margin: 0 auto;
    overflow: hidden;

    p {
      font-size: 24px;
      font-weight: 600;
    }
  }

  .login-card {
    width: 520px;
    max-width: 100%;
    // margin: 100px auto;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }

  .reg {
    float: right;
    // margin-right: 100px;
  }
}
</style>
